package org.jash.shop2202.utils

import androidx.lifecycle.LifecycleOwner

val bus by lazy { SingleLiveData<Pair<String, Any>>() }

fun observerBus(owner: LifecycleOwner, thiz:Any = owner) {
    //类中所有定义的方法
    val methods = thiz.javaClass.declaredMethods
    methods.filter { it.getAnnotation(Event::class.java) != null }
        .forEach { m ->
            val event = m.getAnnotation(Event::class.java)!!
            bus.observe(owner) {
                if (it.first == event.value) {
                    m.invoke(thiz, it.second)
                }
            }
        }
}